package com.gucheng.service.impl;

import com.gucheng.common.ResponseResult;
import com.gucheng.constants.SystemConstants;
import com.gucheng.model.entity.LoginUser;
import com.gucheng.model.entity.User;
import com.gucheng.service.LoginService;
import com.gucheng.utils.JwtUtil;
import com.gucheng.utils.RedisCache;
import com.gucheng.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class SystemLoginServiceImpl implements LoginService {

    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private RedisCache redisCache;
    @Override
    public ResponseResult login(User user) {
        UsernamePasswordAuthenticationToken authenticationToken = new
                UsernamePasswordAuthenticationToken(user.getUserName(),user.getPassword());
        Authentication authenticate =
                authenticationManager.authenticate(authenticationToken);
//判断是否认证通过
        if(Objects.isNull(authenticate)){
            throw new RuntimeException("用户名或密码错误");
        }
//获取userid 生成token
        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
        String userId = loginUser.getUser().getId().toString();
        String jwt = JwtUtil.createJWT(userId);
//把用户信息存入redis
        redisCache.setCacheObject(SystemConstants.LOGIN +userId,loginUser);
//把token封装 返回
        Map<String,String> map = new HashMap<>();
        map.put("token",jwt);
        return ResponseResult.okResult(map);
    }

    @Override
    public ResponseResult logout() {
        Long userId = SecurityUtils.getUserId();
        //删除redis中对应的值
        redisCache.deleteObject(SystemConstants.LOGIN + userId);

        return ResponseResult.okResult();
    }
}
